﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Clothes.Masters.ModuleData
{
    internal class RadarDescriptionsParam : RadarParameters
    {
        public RadarDescriptionsParam(DescriptionType type, int ID)
        {
            Params = new List<object>();
            Params.Add(type);
            Params.Add(ID);
        }
    }

    internal class RadarMastersParam : RadarParameters
    {
        public RadarMastersParam(int ID)
        {
            Params = new List<object>();
            Params.Add(ID);
        }
    }
    #region Radar Descriptions

    internal class RadarDescriptions : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3;
        private DescriptionType type;

        public RadarDescriptions(DescriptionType type)
            : base()
        {
            this.type = type;
            rdrCodeColumn = CL_Descriptions.ID.Name;
            rdrDescColumn = CL_Descriptions.Description.Name;
            switch (type)
            {
                case DescriptionType.E_Title:
                    rdrNameSpace = "Clothes.Clothes.Masters.Forms.Titles";
                    break;
            }
        }

        protected override Table GetRadarTable()
        {
            return new CL_Descriptions();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(CL_Descriptions.Name);
            qb.AddFrom(CL_Descriptions.Name);
            qb.AddCompare(CL_Descriptions.Type, "@p1");
            qb.AddCompare(CL_Descriptions.ID, "@p2");

            p1 = new SqlABParameter("@p1", CL_Descriptions.Type);
            p2 = new SqlABParameter("@p2", CL_Descriptions.ID);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[CL_Descriptions.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", CL_Descriptions.Type);

            qb.Clear();
            qb.AddSelectAll(CL_Descriptions.Name);
            qb.AddFrom(CL_Descriptions.Name);
            qb.AddCompare(CL_Descriptions.Type, "@p3");
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = type;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarDescriptionsParam(type, DataGridViewRowValues.GetValue<int>(row, CL_Descriptions.ID));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarDescriptionsParam(type, int.Parse(text));
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }

    #endregion

    #region Radar Masters

    internal class RadarMasters : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarMasters()
            : base()
        {
            rdrCodeColumn = CL_Masters.ID.Name;
            rdrDescColumn = CL_Masters.Description.Name;
            rdrNameSpace = "Clothes.Clothes.Masters.Forms.Masters";
        }

        protected override Table GetRadarTable()
        {
            return new CL_Masters();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", CL_Masters.ID);

            qb.Clear();
            qb.AddSelectAll(CL_Masters.Name);
            qb.AddFrom(CL_Masters.Name);
            qb.AddCompare(CL_Masters.ID, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[CL_Masters.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(CL_Masters.Name);
            qb.AddFrom(CL_Masters.Name);

            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarMastersParam(DataGridViewRowValues.GetValue<int>(row, CL_Masters.ID));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarMastersParam(int.Parse(text));
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }

        //protected override void FormatColumns(DataGridViewRow row, string columnName)
        //{
        //    if (columnName == FG_Athlete.Club.Name && row.Cells[0].Value != null)
        //    {
        //        string mail = DataGridViewRowValues.GetValue<string>(row, FG_Athlete.Mail);
        //        if (DRClub.Find(mail))
        //            DataGridViewRowValues.SetValue<string>(row, FG_Athlete.Club, DRClub.GetValue<string>(FG_Club.Club));
        //    }
        //}

    }

    #endregion

}